import React from 'react';
import { Button, Modal, Form, Input } from 'antd';

const FormItem = Form.Item;

class StandardToAsk extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      visible: false,
      unknown: '',
      question: '',
      dataQuestion: '',
      list: [],
      loading: false,
    };
  }

  openModal = (test, data) => {
    this.setState({
      unknown: test,
      dataQuestion: data,
      visible: true,
    });
  };

  cancelModal = () => {
    this.setState({
      visible: false,
      question: '',
    });
  };

  okModal = () => {
    const { unknown, dataQuestion } = this.state;
    this.props.form.validateFields((err, values) => {
      if (!err) {
        let question = values.question;
        let data = {
          id: dataQuestion.id,
          qaId: dataQuestion.qaId,
          question: question,
        };
        switch (unknown) {
          case 'delAbnormalQ': // 删除异常问
            this.props.delAbnormalQ(data);
            break;
          case 'delSimilarToAsk': // 删除相似问
            this.props.delSimilarAsk(dataQuestion.items, question, dataQuestion.index);
            break;
          case 'relationAbnormalQ': // 异常问关联知识点
            this.props.relationQa({ ...data, qaType: dataQuestion.qaType, });
            break;
          case 'appendSimilarToAsk': // 相似问添加到>>
            this.props.append(
              dataQuestion,
              dataQuestion.qaId,
              dataQuestion.sQaId,
              dataQuestion.question,
              question
            );
            break;
        }
      }
    });
  };

  componentDidMount() {
    if (this.props.onRef) {
      this.props.onRef(this);
    }
  }

  render() {
    const { visible, list, loading, unknown, dataQuestion } = this.state;
    const { getFieldDecorator } = this.props.form;
    const FormItemLayont = {
      labelCol: {
        xs: 24,
        sm: 4,
        md: 4,
      },
      wrapperCol: {
        xs: 24,
        sm: 20,
        md: 18,
      },
    };
    return (
      <Modal visible={visible} title={'标准问设置'} onCancel={this.cancelModal} onOk={this.okModal}>
        <Form layout="horizontal">
          <div style={{ textAlign: 'center' }}>
            <p style={{ margin: 0 }}>
              问题“
              <span style={{ fontWeight: 600 }}>
                {unknown == 'delSimilarToAsk' ? dataQuestion.item : dataQuestion.question}
              </span>
              ”是其所属知识点的标准问
            </p>
            <p>请先设置新的标准问后再删除</p>
          </div>
          <FormItem label="新标准问" {...FormItemLayont}>
            {getFieldDecorator('question', {
              rules: [
                {
                  message: '请输入新标准问',
                  required: true,
                },
              ],
              initialValue: this.state.question,
            })(<Input />)}
          </FormItem>
        </Form>
      </Modal>
    );
  }
}

export default Form.create()(StandardToAsk);
